##!/bin/bash -x
# deploy
#set MAVEN_OPTS=-Dfile.encoding=UTF-8

PROJECT_NAME=e-shop-example
GITPROJECT_NAME=e-shop-example
POMPROJECT_NAME=e-shop-example
WEB_SERVER_DIR=~/developer/java/apache-tomcat-6.0.43
WARBACKUP=~/developer/java/warbackup
GIT_CODE_DIR=~/developer/java/workspaces/$GITPROJECT_NAME
GIT_URL=https://git.oschina.net/xiaominger/"$PROJECT_NAME".git

PROG_NAME=$0
ACTION=$1
SPACE_STR="..................................................................................................."


#	[ -e $GIT_CODE_DIR ] || mkdir -v $GIT_CODE_DIR
build()
{
	echo "deploying $PROJECT_NAME"
	echo "building code"
	if [ -e $GIT_CODE_DIR ]; then
		echo "cd $GIT_CODE_DIR"
		cd $GIT_CODE_DIR
               # git pull $GIT_URL	
	else    	
		cd $HOME
		echo "svn co $GIT_URL/$GITPROJECT_NAME/"
	        git clone $GIT_URL	
     fi

		echo "delete old deploy package"
		mv $WEB_SERVER_DIR/webapps/$PROJECT_NAME.war $WARBACKUP/$PROJECT_NAME.war
		rm -rf $WEB_SERVER_DIR/webapps/$PROJECT_NAME

		cd $GIT_CODE_DIR
		mvn clean install -Dmaven.test.skip=true
		mv $GIT_CODE_DIR/target/$POMPROJECT_NAME.war $WEB_SERVER_DIR/webapps/$PROJECT_NAME.war
}


help() {
	echo "Usage: $PROG_NAME {build|deploy|}"
	echo "build:only build code ,package tgz file"
	echo "deploy:deploy the tgz file to target hosts ,and restart server"
	echo "no arg: this will do build and deploy operation"
	exit 1;
}


case "$ACTION" in
build)
build
;;
deploy)
deploy
;;
balance)
balance
;;
help)
help
;;
*)
build
;;
esac